﻿using System;
using System.Collections.ObjectModel;

using SBPweb.RIA.Shared.Contracts.Components;
using SBPweb.RIA.Client.Common.Collections;

using SBPweb.PROvibe.Shared.Library.Base;
using SBPweb.PROvibe.Shared.Library.Components;

using SBPweb.PROvibe.Client.Library.Base;

namespace SBPweb.PROvibe.Client.Library.Modularity
{

    public abstract class ClientModuleBase : InitializableBase, IClientModule
    {

        #region Fields

        protected ManagableReadOnlyObservableList<IControl> controls;

        #endregion

        #region Constructor logic

        public ClientModuleBase()
        {
            controls = new ManagableReadOnlyObservableList<IControl>();
            ClientContext = new ClientContext(this);
        }

        protected ClientModuleBase(IModule module)
            : this()
        {
            this.Id = module.Id;
            this.Name = module.Name;
            this.Path = module.Path;
            this.Version = module.Version;
        }

        #endregion

        #region Properties

        public ClientContext ClientContext
        {
            get;
            private set;
        }

        public ReadOnlyCollection<IControl> Controls
        {
            get
            {
                return controls;
            }
        }

        public int Id
        {
            get;
            protected set;
        }

        public string Name
        {
            get;
            protected set;
        }

        public Version Version
        {
            get;
            protected set;
        }

        public string Path
        {
            get;
            protected set;
        }

        #endregion

        #region Methods

        //protected override void OnInitialize()
        //{
        //}

        //protected override void OnTerminate()
        //{
        //}

        public int CompareTo(object obj)
        {
            return CompareTo(obj as INameable);
        }

        public int CompareTo(IIndexable other)
        {
            if (other == null)
            {
                return 0;
            }
            return this.Id.CompareTo(other.Id);
        }
        
        public int CompareTo(INameable other)
        {
            if (other == null)
            {
                return 0;
            }
            return this.Name.CompareTo(other.Name);
        }
        
        public int CompareTo(IVersionable other)
        {
            if (other == null)
            {
                return 0;
            }
            return this.Version.CompareTo(other.Version);
        }
        
        #endregion

    }

}